package com.hsq.androidproject.ui;


import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.example.lenovo.androidproject3.R;
import com.hsq.androidproject.adapter.DetailsAdapter;
import com.hsq.androidproject.entity.Details;
import com.hsq.androidproject.utils.OkHttpClientManager;
import com.google.gson.Gson;
import com.squareup.okhttp.Request;

import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class QXzxFragment extends Fragment {//最新动态
    private ListView listView;
    private RadioButton rb_early, rb_new;
    private RadioGroup rg_group;
    String path = "";

    public QXzxFragment() {
        // Required empty public constructor            #ff5694
    }


    @Override
    public View onCreateView(LayoutInflater inflater, final ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_qxzx, container, false);
        initView(view);
        Bundle bundle = getArguments();    /////ShowHotStartsActivity
        final int sid = bundle.getInt("sid", 0);
        addData("early", sid);
        rg_group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_early:
                        rb_early.setTextColor(Color.parseColor("#ff5694"));
                        rb_new.setTextColor(Color.parseColor("#000000"));
                        addData("early", sid);
                        break;
                    case R.id.rb_new:
                        rb_early.setTextColor(Color.parseColor("#000000"));
                        rb_new.setTextColor(Color.parseColor("#ff5694"));
                        addData("new", sid);

                        break;
                }
            }
        });

        return view;
    }

    private void addData(String type, int sid) {
        if (type.equals("early")) { /*评论   最早*/
            path = "http://api.hanju.koudaibaobao.com/api/star/videos?sid=" + sid + "&sort=t&offset=0&count=80";
            ////http://api.hanju.koudaibaobao.com/api/star/videos?sid=3&sort=t&offset=0&count=20

        } else if (type.equals("new")) {  /*评论   最新*/
            path = "http://api.hanju.koudaibaobao.com/api/star/videos?sid=" + sid + "&sort=h&offset=0&count=80";
        }
        OkHttpClientManager.getAsyn(path, new OkHttpClientManager.ResultCallback<String>() {
            @Override
            public void onError(Request request, Exception e) {
                Toast.makeText(getContext(), "出错啦！", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onResponse(String response) {
                Details details = new Gson().fromJson(response, Details.class);
                List<Details.VideosBean> videosBean = details.getVideos();
                DetailsAdapter detailsAdapter = new DetailsAdapter(videosBean, getContext());
                listView.setAdapter(detailsAdapter);
            }
        });
    }

    private void initView(View view) {
        rb_early = (RadioButton) view.findViewById(R.id.rb_early);
        rb_new = (RadioButton) view.findViewById(R.id.rb_new);
        rg_group = (RadioGroup) view.findViewById(R.id.rg_group);
        listView = (ListView) view.findViewById(R.id.zxListViews);

    }

}
